﻿Imports System.Globalization
Imports System.Net

Public Class Util

#Region "プロパティ"

#End Region

#Region "文字列、オブジェクト、数値うちの変換"

    ''' <summary>
    ''' 変換文字列（obj型）を文字列（string型）に変換します。
    ''' </summary>
    ''' <param name="obj">変換文字列</param>
    ''' <returns>変換後の値</returns>
    ''' <remarks></remarks>
    Public Shared Function ObjToString(ByVal obj As Object) As String
        If obj Is Nothing OrElse obj Is DBNull.Value Then
            Return String.Empty
        Else
            Return Convert.ToString(obj)
        End If
    End Function

    ''' <summary>
    ''' 変換文字列（obj型）をDate型に変換します。
    ''' </summary>
    ''' <param name="obj">変換文字列</param>
    ''' <returns>変換後の値</returns>
    ''' <remarks></remarks>
    Public Shared Function ObjToDate(ByVal obj As Object) As Date
        If obj Is Nothing OrElse obj Is DBNull.Value Then
            Return Nothing
        Else
            Return CDate(obj)
        End If
    End Function

    ''' <summary>
    ''' 文字列（obj型）を数値（Integer型）に変換します。
    ''' </summary>
    ''' <param name="obj">変換文字列</param>
    ''' <param name="tryError">変換エラー可否を戻る、省略可</param>
    ''' <returns>変換後の値。変換エラー場合、ゼロを戻る</returns>
    Public Shared Function ObjToInteger(ByVal obj As Object, Optional ByRef tryError As Boolean = False) As Integer
        Dim intRet As Integer = 0
        Dim strObj As String = ObjToString(obj)
        tryError = Integer.TryParse(strObj, NumberStyles.Number, Nothing, intRet)
        Return intRet
    End Function

    ''' <summary>
    ''' 文字列（obj型）を数値（decimal型）に変換します。
    ''' </summary>
    ''' <param name="obj">変換文字列</param>
    ''' <param name="tryError">変換エラー可否を戻る、省略可</param>
    ''' <returns>変換後数値。変換エラー場合、ゼロを戻る</returns>
    Public Shared Function ObjToDecimal(ByVal obj As Object, Optional ByRef tryError As Boolean = False) As Decimal
        Dim decRet As Decimal = 0D
        Dim strObj As String = ObjToString(obj)
        tryError = Decimal.TryParse(strObj, NumberStyles.Number, Nothing, decRet)
        Return decRet
    End Function

    ''' <summary>
    ''' 文字列（obj型）を日時（string型）に変換します。
    ''' </summary>
    ''' <param name="obj">変換文字列</param>
    ''' <param name="strFormat">フォーマット文字列</param>
    ''' <returns>変換後文字列</returns>
    Public Shared Function ObjToDateTimeStr(ByVal obj As Object, Optional ByVal strFormat As String = "yyyyMMddHHmmssfff") As String
        Dim strResult As String = ObjToString(obj)

        If String.IsNullOrEmpty(strResult) Then
            Return String.Empty
        End If

        Dim result As DateTime
        If DateTime.TryParseExact(strResult, "yyyyMMddHHmmssfff", Nothing, DateTimeStyles.None, result) = False Then
            Return String.Empty
        End If

        Return result.ToString("yyyy/MM/dd HH:mm:ss")

    End Function

#End Region

#Region "郵便、電話フォーマット"

    ''' <summary>
    ''' 郵便フォーマット
    ''' </summary>
    ''' <param name="obj"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function ObjFormatToZip(ByVal obj As Object, _
                                          Optional ByVal strStart As String = "〒", _
                                          Optional ByVal strSplit As String = "-") As String
        Dim retStr As String = ObjToString(obj)

        If retStr.Length < 4 Then
            retStr = strStart & retStr
        Else
            retStr = strStart & retStr.Insert(3, strSplit)
        End If

        Return retStr
    End Function

    ''' <summary>
    ''' 電話、ファックスフォーマット
    ''' </summary>
    ''' <param name="obj"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function ObjFormatToTel(ByVal obj As Object, Optional ByVal strSplit As String = "-") As String
        Dim retStr As String = ObjToString(obj)

        If retStr.Length < 4 Then
            Return retStr
        ElseIf retStr.Length < 7 Then
            retStr = retStr.Insert(2, strSplit)
        Else
            retStr = retStr.Insert(6, strSplit).Insert(2, strSplit)
        End If

        Return retStr
    End Function
#End Region

#Region "SQL文"

    ''' <summary>
    ''' Stringは""の場合、DBNull.Valueを戻る
    ''' </summary>
    ''' <param name="strValue"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function StringToSql(ByVal strValue As Object) As Object
        If strValue Is DBNull.Value Then
            Return DBNull.Value
        End If

        If String.IsNullOrEmpty(strValue) Then
            Return DBNull.Value
        Else
            Return strValue.Trim()
        End If
    End Function

    ''' <summary>
    ''' Dateは""の場合、DBNull.Valueを戻る
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function DateToSql(ByVal value As Object) As Object
        If value Is DBNull.Value Then
            Return DBNull.Value
        End If

        If String.IsNullOrEmpty(value) Then
            Return DBNull.Value
        End If

        Return CDate(value)
    End Function

    Public Shared Function IntegerToSql(ByVal value As Object) As Object
        If value Is DBNull.Value Then
            Return DBNull.Value
        End If

        If String.IsNullOrEmpty(value) Then
            Return DBNull.Value
        End If

        Return ObjToInteger(value)
    End Function

    Public Shared Function DecimalToSql(ByVal value As Object) As Object
        If value Is DBNull.Value Then
            Return DBNull.Value
        End If

        If String.IsNullOrEmpty(value) Then
            Return DBNull.Value
        End If

        Return ObjToDecimal(value)
    End Function

    Public Shared Function DecimalToSql(ByVal value As DBNull) As Object
        Return DBNull.Value
    End Function

#End Region

#Region "端数処理"

    ''' <summary>
    ''' 端数処理(decimal)
    ''' </summary>
    ''' <param name="objValue">処理対象(string,decimal)</param>
    ''' <param name="scale">端数処理の位置(マイナス指定で小数位置)</param>
    ''' <param name="roundKbn">端数区分(1:四捨五入,2:切捨て,3:切上げ)</param>
    ''' <returns></returns>
    Public Shared Function CalcRounding(ByVal objValue As Object, ByVal scale As Integer, ByVal roundKbn As Integer) As Decimal

        Dim retVal As Decimal = Util.ObjToDecimal(objValue)

        Dim mCoef As Decimal = 1

        '係数
        '位置合わせのための係数算出
        '位置がマイナスの場合は 1/10 掛ける
        Dim mPow As Decimal = If((scale < 0), 1 / 10D, 10D)
        For i As Integer = 0 To Math.Abs(scale) - 1
            mCoef *= mPow
        Next

        '符号切替
        Dim isNagate As Boolean = (retVal < 0)
        retVal = Math.Abs(retVal)

        If roundKbn = 1 Then
            '四捨五入
            retVal = Math.Floor((retVal / mCoef) + 0.5D) * mCoef
        ElseIf roundKbn = 2 Then
            '切捨て
            retVal = Math.Floor(retVal / mCoef) * mCoef
        ElseIf roundKbn = 3 Then
            '切上げ
            retVal = Math.Ceiling(retVal / mCoef) * mCoef
        End If

        '符号復元
        If isNagate Then
            retVal *= -1
        End If

        Return retVal
    End Function

#End Region

#Region "関数(Private)"

#End Region

End Class
